<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="开卡时间范围" label-width="100">
        <el-date-picker
          @change="dateRangeChange(1)"
          v-model="dateRangeData[0]"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[
            new Date(2000, 1, 1, 0, 0, 0),
            new Date(2000, 1, 1, 23, 59, 59),
          ]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="半年到期时间范围" label-width="130">
        <el-date-picker
          @change="dateRangeChange(2)"
          v-model="dateRangeData[1]"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[
            new Date(2000, 1, 1, 0, 0, 0),
            new Date(2000, 1, 1, 23, 59, 59),
          ]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="一年到期时间范围" label-width="130">
        <el-date-picker
          @change="dateRangeChange(3)"
          v-model="dateRangeData[2]"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[
            new Date(2000, 1, 1, 0, 0, 0),
            new Date(2000, 1, 1, 23, 59, 59),
          ]"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="军区" prop="militaryArea" label-width="100">
        <el-input
          v-model="queryParams.militaryArea"
          placeholder="军区"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="门店名称" prop="shopName" label-width="100">
        <el-input
          v-model="queryParams.shopName"
          placeholder="门店名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="老师" prop="teacherName" label-width="100">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="老师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="会员卡号" prop="membershipCardNumber" label-width="100">
        <el-input
          v-model="queryParams.membershipCardNumber"
          placeholder="会员卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="membershipName" label-width="100">
        <el-input
          v-model="queryParams.membershipName"
          placeholder="客户姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否扣钱" prop="isDeductMoney" label-width="100">
        <el-select clearable @change="handleQuery" v-model="queryParams.isDeductMoney" placeholder="请选择">
          <el-option label="是" value="是"></el-option>
          <el-option label="否" value="否"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="扣钱状态" prop="withholdingStatus" label-width="100">
        <el-select clearable @change="handleQuery" v-model="queryParams.withholdingStatus" placeholder="请选择">
          <el-option
            v-for="dict in sys_withholding_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:yearCardData:export']"
        >导出</el-button>
        <el-button
          type="primary"
          icon="Edit"
          :disabled="single"
          @click="openEdit"
          v-hasPermi="['business:yearCardData:edit']"
        >批量编辑</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="hairTriggerEventList"
       @selection-change="handleSelectionChange"
       row-key="id"
    >
      <el-table-column :reserve-selection="true" type="selection" width="55" />
      <el-table-column label="老师" align="center" prop="teacherName" />
      <el-table-column label="门店" align="center" prop="shopName" />
      <el-table-column label="军区" align="center" prop="militaryArea" />
      <el-table-column label="会员卡号" align="center" prop="membershipCardNumber" />
      <el-table-column label="客户姓名" align="center" prop="membershipName" />
      <el-table-column label="年卡类型" align="center" prop="yearCardType" />
      <el-table-column label="金额" align="center" prop="actualAmount" />
      <el-table-column label="开卡时间" align="center" prop="cardOpenTime" />
      <el-table-column label="半年扣费时间" align="center" prop="halfYearlyDeductionTime" />
      <el-table-column label="一年到期时间" align="center" prop="expireTime" />
      <el-table-column label="赠送次数" align="center" prop="giftsNum" />
      <el-table-column label="基准次数" align="center" prop="datumNum" />
      <el-table-column label="上半年消费次数" align="center" prop="firstHalfYearConsumeNum" />
      <el-table-column label="下半年消费次数" align="center" prop="secondHalfYearConsumeNum" />
      <el-table-column label="上半年扣钱" align="center" prop="firstHalfYearMoney" />
      <el-table-column label="下半年扣钱" align="center" prop="secondHalfYearMoney" />
<!--      <el-table-column label="消费次数" align="center" prop="consumeNum" />-->
<!--      <el-table-column label="是否扣钱" align="center" prop="isDeductMoney" />-->
      <el-table-column label="扣钱状态" align="center" prop="withholdingStatus" />
      <el-table-column label="备注" align="center" prop="remake" />
      <el-table-column fixed="right" width="100" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button  type="primary" @click="handleDetails(scope.row)" v-hasPermi="['business:yearCardDetails:query']">详情</el-button>
          <br />
          <div class="margin-top: 10px">
            <el-button  type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['business:yearCardDeta:update']">编辑</el-button>
          </div>


        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form
        ref="hairRunescapeRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="扣钱状态" prop="withholdingStatus">
          <el-select v-model="form.withholdingStatus" placeholder="请选择店铺类型">
            <el-option
              v-for="dict in sys_withholding_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remake">
          <el-input type="textarea" :rows="4" v-model="form.remake" placeholder="备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="HairTriggerEvent">
import {
 getYearCardData, updateYearCard,batchUpdateYearCard
} from "@/api/business/operation";
import {useRouter} from "vue-router";
const router = useRouter();
const { proxy } = getCurrentInstance();
const { sys_withholding_status } = proxy.useDict("sys_withholding_status");
const hairTriggerEventList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const total = ref(0);
const title = ref("");
// const dateRange = ref([proxy.parseTime(new Date(), "{y}-{m}-{d}") + ' 00:00:00' ,proxy.parseTime(new Date(), "{y}-{m}-{d}") + ' 23:59:59']);
const dateRange = ref([]);
const dateRangeData = ref([dateRange,dateRange,dateRange]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 15,
    nickName: undefined,
    queryUser: "1",
    salesDirector: undefined,
    eventValue: undefined,
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户触发事件记录列表 */
function getList() {
  loading.value = true;
  console.log(proxy.addDateRange(queryParams.value, dateRangeData.value[queryParams.value.dateRangeType - 1]))
  getYearCardData(proxy.addDateRange(queryParams.value, dateRangeData.value[queryParams.value.dateRangeType - 1])).then((response) => {
    hairTriggerEventList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = !selection.length;
}
function openEdit() {
  if(ids.value.length>0){
    loading.value = true;
    reset();
    loading.value = false;
    open.value = true;
    title.value = "批量编辑";
  }
}
function dateRangeChange(type){
  queryParams.value.dateRangeType = type
}
function handleDetails(row){
  console.log(row)
  router.push({
    path: "/finance/annualCardData/yearCardDetails",
    query: { membershipCardNumber: row.membershipCardNumber },
  });
}

function handleUpdate(row) {
  loading.value = true;
  reset();
  const _id = row.id || ids.value;
  loading.value = false;
  form.value.id = row.id;
  form.value.withholdingStatus = row.withholdingStatus;
  form.value.remake = row.remake;
  open.value = true;
  title.value = "编辑";
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  dateRangeData.value = [];
  queryParams.value = {
    pageNum: 1,
    pageSize: 15,
    userId: null,
    dateRangeType: null,
    type: null,
    startDate: null,
    endDate: null,
    nickName: null,
    salesDirector: null,
    eventValue: null,
    userType: null
  };
  handleQuery();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    withholdingStatus: null,
    remake: null,
  };
  proxy.resetForm("hairRunescapeRef");
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["hairRunescapeRef"].validate((valid) => {
    buttonLoading.value = true;
    if(title.value=='编辑'){
      updateYearCard(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
        regionValue.value = [];
      }).finally(() => {
        buttonLoading.value = false;
      });
    }else{
      form.value.ids = ids.value
      batchUpdateYearCard(form.value).then(() => {
        form.value.ids = undefined
        proxy.$modal.msgSuccess("批量修改成功");
        open.value = false;
        getList();
      }).finally(() => {
        buttonLoading.value = false;
      });
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/operation/exportYearCardData",
    {
      ...queryParams.value,
    },
    `accountStatement_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
